<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "familymember".
 *
 * @property integer $id
 * @property string $stu_id
 * @property string $relate
 * @property string $name
 * @property string $date_of_birth
 * @property string $workplace
 * @property string $duties
 * @property string $telephone
 */
class FamilymemberModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'familymember';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id','relate','name','date_of_birth','workplace','duties','telephone'], 'required'],
            [['id'], 'integer'],
            [['stu_id', 'relate', 'date_of_birth'], 'string', 'max' => 20],
            [['name', 'duties'], 'string', 'max' => 50],
        		[['stu_name'], 'string', 'max' => 50],
            [['workplace'], 'string', 'max' => 100],
        	['telephone','string','max'=>11,'length'=>11]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            //'id' => 'ID',
            'stu_id' => '学号',
        	'stu_name'=>'姓名',
            'relate' => '关系',
            'name' => '成员姓名',
            'date_of_birth' => '出生日期',
            'workplace' => '工作单位',
            'duties' => '职务',
            'telephone' => '联系电话',
        ];
    }


    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new FamilymemberModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->stu_name=$value['姓名'].'';
                $person->relate=$value['关系'].'';
                $person-> name=$value['成员姓名'];
                $person->date_of_birth=$value['出生日期'].'';
                $person->workplace=$value['工作单位'].'';
                $person->duties=$value['职务'].'';
                $person->telephone=$value['联系电话'].'';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }
}
